package com.example.librarysystem.Controller;

import com.example.librarysystem.Dao.BooksDao;
import com.example.librarysystem.Service.*;
import com.example.librarysystem.entity.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Api(value = "游客控制器接口")
public class VisitorController {
    @Autowired
    private BooksService booksService;
    @Autowired
    private BorrowService borrowService;
    @Autowired
    private StarService starService;
    @Autowired
    private TimesService timesService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private BorrowerService borrowerService;

    @ApiOperation(value = "游客欢迎页面")
    @GetMapping("/visitorWelcome")
    public String visitorWelcome(Model model){
        Integer borrowerNum = borrowerService.selectCount();
        Integer borrowNum = borrowService.selectCount();
        Integer commentNum = commentService.selectCount();
        Integer bookNum = booksService.selectCount();
        model.addAttribute("borrowerNum", borrowerNum);
        model.addAttribute("borrowNum",borrowNum);
        model.addAttribute("commentNum",commentNum);
        model.addAttribute("bookNum",bookNum);
        return "page/visitorWelcome";
    }

    @ApiOperation(value = "游客图书查看")
    @GetMapping("visitorBookShow")
    public String visitorBookShow(){
        System.out.println("visitorBookShow");
        return "visitor/visitorBookShow";
    }
    @ApiOperation(value = "图书信息查询")
    @GetMapping("/visitorBooksInfo")
    @ResponseBody
    public JsonObject visitorBooksInfo(int page, int limit,
                                       @RequestParam(value="type", required = false) String type){
        JsonObject jsonObject =new JsonObject();
        jsonObject.setCode(0);
        jsonObject.setMsg("ok");
        jsonObject.setCount((long)booksService.selectCount());
        List<Books> booksList = null;
        if(type == null){
            booksList = booksService.selectAllByLimit(page, limit);
        } else {
            String c = '%'+type+'%';
            booksList = booksService.selectAllByClassification(c,page,limit);
            System.out.println(booksList);
        }
        jsonObject.setData(booksList);
        return jsonObject;
    }
    @ApiOperation(value = "游客图书详情")
    @GetMapping("/visitorBooksDetail")
    public String visitorBooksDetail(int id , Model model){
        Books books = booksService.selectById(id);
        if(books != null){
            Times times = timesService.selectByName(books.getName());
            times.setClickTimes(times.getClickTimes() + 1);
            timesService.updateClickTimes(times);
            List<Books> booksList = booksService.selectByISBN1(books.getISBN());
            System.out.println(booksList);
            List<Books> sameBorrow = booksService.selectBySameBorrower(books.getName());
            List<Books> sameStar = booksService.selectBySameStar(books.getName());
            Integer star = starService.selectCountByBookName(books.getName());
            Integer borrow = borrowService.selectCountByBookName(books.getName());
            List<Comment> commentList = commentService.selectByBookName(books.getName());
            for (Comment comment : commentList) {
                String temp = comment.getBorrowerName().substring(0, 1);
                temp = temp + "**";
                comment.setBorrowerName(temp);
            }
            model.addAttribute("books", books);
            model.addAttribute("same", booksList);
            model.addAttribute("sameBorrow",sameBorrow);
            model.addAttribute("sameStar", sameStar);
            model.addAttribute("star", star);
            model.addAttribute("borrow", borrow);
            model.addAttribute("comment", commentList);
        }
        return "visitor/visitorBookDetail";
    }
    @ApiOperation(value = "游客图书搜索页面")
    @GetMapping("/visitorBookSearch")
    public String visitorBookSearch(){
        return "visitor/visitorBookSearch";
    }

    @ApiOperation(value = "游客图书搜索")
    @GetMapping("/visitorBooksSearchDetail")
    @ResponseBody
    public JsonObject visitorBooksSearch(int page, int limit, int op, String title){
        JsonObject jsonObject =new JsonObject();
        jsonObject.setCode(0);
        jsonObject.setMsg("ok");
        jsonObject.setCount((long)booksService.selectCount());
        List<Books> booksList = null;
        if(op == 0){
            title = '%' + title + '%';
            booksList = booksService.selectByName(title,page,limit);
        }else if(op == 1){
            title = '%' + title + '%';
            booksList = booksService.selectByAuthor(title,page,limit);
        }else if(op == 2){
            title = '%' + title + '%';
            booksList = booksService.selectByPublishingHouse(title,page,limit);
        }else if(op == 3){
            title = '%' + title + '%';
            booksList = booksService.selectByPublishDate(title,page,limit);
        }else if(op == 4){
            title = '%' + title + '%';
            booksList = booksService.selectByISBN(title,page,limit);
        }
        System.out.println(booksList);
        if(booksList != null){
            for(Books book : booksList){
                Times times = timesService.selectByName(book.getName());
                times.setSearchTimes(times.getSearchTimes() + 1);
                timesService.updateSearchTimes(times);
            }
        }
        //System.out.println(booksList);
        jsonObject.setData(booksList);
        return jsonObject;
    }
    @ApiOperation(value = "游客图书排名")
    @GetMapping ("/visitorBookRank")
    public String visitorBookRank(Model model){
        List<Books> borrowRank = booksService.selectBookByBorrowDesc(1, 10);
        List<Books> searchRank = booksService.selectBookBySearchDesc(1, 10);
        List<Books> starRank = booksService.selectBookByStarDesc(1, 10);
        List<Books> scoreRank = booksService.selectByScoreDesc(1, 10);
        List<Books> clickRank = booksService.selectBookByClickDesc(1, 10);
        model.addAttribute("borrowRank", borrowRank);
        model.addAttribute("searchRank", searchRank);
        model.addAttribute("starRank", starRank);
        model.addAttribute("scoreRank", scoreRank);
        model.addAttribute("clickRank", clickRank);
        return "visitor/visitorBookRank";
    }
    @ApiOperation(value = "游客图书排名方式")
    @GetMapping("/visitorBooksRankInfo")
    @ResponseBody
    public JsonObject visitorBooksRankInfo(int page, int limit, int id){
        JsonObject jsonObject =new JsonObject();
        jsonObject.setCode(0);
        jsonObject.setMsg("ok");
        jsonObject.setCount((long)booksService.selectCount());
        List<Books> booksList = null;
        if(id == 1){
            booksList = booksService.selectBookByBorrowDesc(page, limit);
        }else if(id == 2){
            booksList = booksService.selectBookBySearchDesc(page, limit);
        }else if(id == 3){
            booksList = booksService.selectBookByStarDesc(page, limit);
        }else if(id == 4){
            booksList = booksService.selectByScoreDesc(page, limit);
        }else if(id == 5){
            booksList = booksService.selectBookByClickDesc(page, limit);
        }
        jsonObject.setData(booksList);
        return jsonObject;
    }
}
